<?php
/* Author: tbloc@cantho.gov.vn */
/* Created Date : 12-05-2014 14:47:20 */
class Kiem_ke{  
//Properties
private $KK_ID;
private $TUAN_ID;
private $THANG_ID;
private $BEP_ID;
private $KK_NGAY_KIEM_KE_BEGIN;
private $KK_NGAY_KIEM_KE_END;
private $KK_TONG_KHO;
private $IS_ACTIVE;
 private $MIN_KK_NGAY_KIEM_KE_BEGIN;
    private $MAX_KK_NGAY_KIEM_KE_BEGIN;
    public function getMIN_KK_NGAY_KIEM_KE_BEGIN() {
        return $this->MIN_KK_NGAY_KIEM_KE_BEGIN;
    }

    public function getMAX_KK_NGAY_KIEM_KE_BEGIN() {
        return $this->MAX_KK_NGAY_KIEM_KE_BEGIN;
    }

    public function setMIN_KK_NGAY_KIEM_KE_BEGIN($MIN_KK_NGAY_KIEM_KE_BEGIN) {
        $this->MIN_KK_NGAY_KIEM_KE_BEGIN = $MIN_KK_NGAY_KIEM_KE_BEGIN;
    }

    public function setMAX_KK_NGAY_KIEM_KE_BEGIN($MAX_KK_NGAY_KIEM_KE_BEGIN) {
        $this->MAX_KK_NGAY_KIEM_KE_BEGIN = $MAX_KK_NGAY_KIEM_KE_BEGIN;
    }


//properties set get 
public function setKK_ID($KK_ID) 
{
	$this->KK_ID = $KK_ID;
}
public function getKK_ID()
{
	return $this->KK_ID;
}	        
public function setTUAN_ID($TUAN_ID) 
{
	$this->TUAN_ID = $TUAN_ID;
}
public function getTUAN_ID()
{
	return $this->TUAN_ID;
}	        
public function setTHANG_ID($THANG_ID) 
{
	$this->THANG_ID = $THANG_ID;
}
public function getTHANG_ID()
{
	return $this->THANG_ID;
}	        
public function setBEP_ID($BEP_ID) 
{
	$this->BEP_ID = $BEP_ID;
}
public function getBEP_ID()
{
	return $this->BEP_ID;
}	        
public function setKK_NGAY_KIEM_KE_BEGIN($KK_NGAY_KIEM_KE_BEGIN) 
{
	$this->KK_NGAY_KIEM_KE_BEGIN = $KK_NGAY_KIEM_KE_BEGIN;
}
public function getKK_NGAY_KIEM_KE_BEGIN()
{
	return $this->KK_NGAY_KIEM_KE_BEGIN;
}	        
public function setKK_NGAY_KIEM_KE_END($KK_NGAY_KIEM_KE_END) 
{
	$this->KK_NGAY_KIEM_KE_END = $KK_NGAY_KIEM_KE_END;
}
public function getKK_NGAY_KIEM_KE_END()
{
	return $this->KK_NGAY_KIEM_KE_END;
}	        
public function setKK_TONG_KHO($KK_TONG_KHO) 
{
	$this->KK_TONG_KHO = $KK_TONG_KHO;
}
public function getKK_TONG_KHO()
{
	return $this->KK_TONG_KHO;
}	        
public function setIS_ACTIVE($IS_ACTIVE) 
{
	$this->IS_ACTIVE = $IS_ACTIVE;
}
public function getIS_ACTIVE()
{
	return $this->IS_ACTIVE;
}	        
//crud methods
public function insert($db = null) {
	if ($db != null){
	$sql = "INSERT INTO `kiem_ke`(                        
			`KK_ID`, 
				`TUAN_ID`, 
				`THANG_ID`, 
				`BEP_ID`, 
				`KK_NGAY_KIEM_KE_BEGIN`, 
				`KK_NGAY_KIEM_KE_END`, 
				`KK_TONG_KHO`, 
				`IS_ACTIVE` 
		)
	VALUES(
    		:KK_ID, 
	    		:TUAN_ID, 
	    		:THANG_ID, 
	    		:BEP_ID, 
	    		:KK_NGAY_KIEM_KE_BEGIN, 
	    		:KK_NGAY_KIEM_KE_END, 
	    		:KK_TONG_KHO, 
	    		:IS_ACTIVE 
	    );";
	$parameters = array(
			":KK_ID", 
				":TUAN_ID", 
				":THANG_ID", 
				":BEP_ID", 
				":KK_NGAY_KIEM_KE_BEGIN", 
				":KK_NGAY_KIEM_KE_END", 
				":KK_TONG_KHO", 
				":IS_ACTIVE" 
		);
    $values = array(  
            	$this->getKK_ID(), 
	            	$this->getTUAN_ID(), 
	            	$this->getTHANG_ID(), 
	            	$this->getBEP_ID(), 
	            	$this->getKK_NGAY_KIEM_KE_BEGIN(), 
	            	$this->getKK_NGAY_KIEM_KE_END(), 
	            	$this->getKK_TONG_KHO(), 
	            	$this->getIS_ACTIVE()
	    );
    $types = array(
   
                                    PDO::PARAM_INT,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                        PDO::PARAM_INT
                       );
	$lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
	$this->setKK_ID($lastInsertID);
    }
}
	
	public function delete($db = null) {
	    if ($db != null) {
	        $sql = "update `kiem_ke`
	                set `is_active`=0
	                where `KK_ID` = :KK_ID;";
	        $parameters = array(":KK_ID");
	        $values = array($this->getKK_ID());
	        $types = array(PDO::PARAM_INT);//Kiem tra lai kieu du lieu nhe
	        $result = $db->executeNonQuery($sql, $parameters, $values, $types);
	        return $result;
	    }
	}
	
	public function update($db = null) {
	        if ($db != null) {
	            $sql = "UPDATE `kiem_ke` SET 
			`KK_ID`=:KK_ID,
				`TUAN_ID`=:TUAN_ID,
				`THANG_ID`=:THANG_ID,
				`BEP_ID`=:BEP_ID,
				`KK_NGAY_KIEM_KE_BEGIN`=:KK_NGAY_KIEM_KE_BEGIN,
				`KK_NGAY_KIEM_KE_END`=:KK_NGAY_KIEM_KE_END,
				`KK_TONG_KHO`=:KK_TONG_KHO,
				`IS_ACTIVE`=:IS_ACTIVE
			WHERE `KK_ID` = :KK_ID;";
	            $parameters = array(
	        	":KK_ID",
		        	":TUAN_ID",
		        	":THANG_ID",
		        	":BEP_ID",
		        	":KK_NGAY_KIEM_KE_BEGIN",
		        	":KK_NGAY_KIEM_KE_END",
		        	":KK_TONG_KHO",
		        	":IS_ACTIVE"
		        );	
	$values = array(  
	        	$this->getKK_ID(),
        	        	$this->getTUAN_ID(),
        	        	$this->getTHANG_ID(),
        	        	$this->getBEP_ID(),
        	        	$this->getKK_NGAY_KIEM_KE_BEGIN(),
        	        	$this->getKK_NGAY_KIEM_KE_END(),
        	        	$this->getKK_TONG_KHO(),
        	                $this->getIS_ACTIVE()      
        	);
    $types = array(
                                    PDO::PARAM_INT,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_INT
                       );
	$result = $db->executeNonQuery($sql, $parameters, $values, $types);
	return $result;
	        }
	 }
	 
	public function getByID($db = null) {
	if ($db != null) {
	$sql = "SELECT
			`KK_ID`,
				`TUAN_ID`,
				`THANG_ID`,
				`BEP_ID`,
				`KK_NGAY_KIEM_KE_BEGIN`,
				`KK_NGAY_KIEM_KE_END`,
				`KK_TONG_KHO`,
				`IS_ACTIVE`
			FROM `kiem_ke` WHERE `KK_ID` = :KK_ID and `is_active`=1;";
	$parameters = array(":KK_ID");
	$values = array($this->getKK_ID());
	$types = array(PDO::PARAM_INT);//kiem tra lai kieu du lieu nhe
	$rows = $db->executeQuery($sql, $parameters, $values, $types);
	if ($rows != null) {
	$row = $rows->fetch();
		$this->setKK_ID($row->KK_ID);
		$this->setTUAN_ID($row->TUAN_ID);
		$this->setTHANG_ID($row->THANG_ID);
		$this->setBEP_ID($row->BEP_ID);
		$this->setKK_NGAY_KIEM_KE_BEGIN($row->KK_NGAY_KIEM_KE_BEGIN);
		$this->setKK_NGAY_KIEM_KE_END($row->KK_NGAY_KIEM_KE_END);
		$this->setKK_TONG_KHO($row->KK_TONG_KHO);
		$this->setIS_ACTIVE($row->IS_ACTIVE);
	}
	return $this;
		}
	}
	
        
        


    public function getMaxID_($db = null) {
        if ($db != null) {
            $sql = "SELECT MAX(`kiem_ke`.`KK_ID`) AS  KK_ID,`kiem_ke`.`KK_NGAY_KIEM_KE_BEGIN`,`kiem_ke`.`TUAN_ID`,`kiem_ke`.`THANG_ID` FROM `kiem_ke` WHERE `kiem_ke`.`IS_ACTIVE`=1";
            $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setKK_ID($row->KK_ID);
                $this->setKK_NGAY_KIEM_KE_BEGIN($row->KK_NGAY_KIEM_KE_BEGIN);
                $this->setTUAN_ID($row->TUAN_ID);
                $this->setTHANG_ID($row->THANG_ID);
            }
            return $this;
        }
    }
        
        
        
        
           public function getMaxTuan($db = null) {
        if ($db != null) {
            $sql = "SELECT MAX(`kiem_ke`.`TUAN_ID`) AS  TUAN_ID FROM `kiem_ke` WHERE `THANG_ID`=:THANG_ID AND `kiem_ke`.`IS_ACTIVE`=1";
             $parameters = array(":THANG_ID");
            $values = array($this->getTHANG_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setTUAN_ID($row->TUAN_ID);
            }
            return $this;
        }
    }
    
    
    
    public function getMaxThang($db = null) {
        if ($db != null) {
            $sql = "SELECT MAX(`kiem_ke`.`THANG_ID`) AS  THANG_ID FROM `kiem_ke` WHERE `kiem_ke`.`IS_ACTIVE`=1";
            $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setTHANG_ID($row->THANG_ID);
            }
            return $this;
        }
    }
        
         public function showdulieu($idbep,$thang,$tuan,$db = null) {
        if ($db != null) {
            $sql = "SELECT SUM(`KK_TONG_KHO`) AS TONG_TIEN,MAX(`KK_NGAY_KIEM_KE_BEGIN`) AS MAX_KK_NGAY_KIEM_KE_BEGIN,MIN(`KK_NGAY_KIEM_KE_BEGIN`) AS MIN_KK_NGAY_KIEM_KE_BEGIN
            FROM `kiem_ke`
            WHERE tuan_id = '".$tuan."' AND thang_id = '".$thang."' AND BEP_ID = '".$idbep."' AND `is_active`=1;";
           $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setKK_TONG_KHO($row->TONG_TIEN);
                $this->setMAX_KK_NGAY_KIEM_KE_BEGIN($row->MAX_KK_NGAY_KIEM_KE_BEGIN);
                $this->setMIN_KK_NGAY_KIEM_KE_BEGIN($row->MIN_KK_NGAY_KIEM_KE_BEGIN);
            }
            return $this;
        }
    }
     public function showdulieu_thang($idbep,$thang,$db = null) {
        if ($db != null) {
            $sql = "SELECT SUM(`KK_TONG_KHO`) AS TONG_TIEN,MAX(`KK_NGAY_KIEM_KE_BEGIN`) AS MAX_KK_NGAY_KIEM_KE_BEGIN,MIN(`KK_NGAY_KIEM_KE_BEGIN`) AS MIN_KK_NGAY_KIEM_KE_BEGIN
            FROM `kiem_ke`
            WHERE thang_id = '".$thang."' AND BEP_ID = '".$idbep."' AND `is_active`=1;";
           $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setKK_TONG_KHO($row->TONG_TIEN);
                $this->setMAX_KK_NGAY_KIEM_KE_BEGIN($row->MAX_KK_NGAY_KIEM_KE_BEGIN);
                $this->setMIN_KK_NGAY_KIEM_KE_BEGIN($row->MIN_KK_NGAY_KIEM_KE_BEGIN);
            }
            return $this;
        }
    }
     public function showdulieu_tungay_denngay($BEP_ID,$tungay, $denngay,$db = null) {
        if ($db != null) {
            $sql = "SELECT SUM(`KK_TONG_KHO`) AS TONG_TIEN,`kiem_ke`.`BEP_ID`
            FROM `kiem_ke`
            WHERE `KK_NGAY_KIEM_KE_BEGIN`>='".$tungay."' AND `KK_NGAY_KIEM_KE_BEGIN`<='".$denngay."' AND `BEP_ID` = ".$BEP_ID." AND `is_active`=1;";
           $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
               $this->setKK_TONG_KHO($row->TONG_TIEN);
                 $this->setBEP_ID($row->BEP_ID);
            }
            return $this;
        }
    }
        
        
        
	public function getList($db = null) {
	if ($db != null) {
    $sql = "SELECT
			`KK_ID`,
				`TUAN_ID`,
				`THANG_ID`,
				`BEP_ID`,
				`KK_NGAY_KIEM_KE_BEGIN`,
				`KK_NGAY_KIEM_KE_END`,
				`KK_TONG_KHO`,
				`IS_ACTIVE`
			FROM `kiem_ke` WHERE `is_active`=1;";
    $rows = $db->executeQuery($sql);
    $objects = array();
    if ($rows != null) {
    while ($row = $rows->fetch()) {
    $obj = new kiem_ke();
    	 $obj->setKK_ID($row->KK_ID);
		 $obj->setTUAN_ID($row->TUAN_ID);
		 $obj->setTHANG_ID($row->THANG_ID);
		 $obj->setBEP_ID($row->BEP_ID);
		 $obj->setKK_NGAY_KIEM_KE_BEGIN($row->KK_NGAY_KIEM_KE_BEGIN);
		 $obj->setKK_NGAY_KIEM_KE_END($row->KK_NGAY_KIEM_KE_END);
		 $obj->setKK_TONG_KHO($row->KK_TONG_KHO);
		 $obj->setIS_ACTIVE($row->IS_ACTIVE);
		$objects[] = $obj;
    }
    return $objects;
    }
        }
    }
}
?>

